<?php

    class Core extends CI_Controller 
    {

        function __construct() { parent::__construct(); }

        function index() { show_404(); }

        function maillist_submit(){

            $rows = $this->db->get_where('maillist', array('email' => $this->input->post('maillist_email')))->result_array();

            if(count($rows)>0){
                echo json_encode(array('action'=>false,'msg'=>'Alredy registered email.'));
            }else{
                $this->db->insert('maillist', array(
                        'email' => $this->input->post('maillist_email'),
                        'created' => time()
                    ));

                echo json_encode(array('action'=>true,'msg'=>'Succefull. Thank you for registring with us.'));
            } 

        }

        function notification_mail(){

            $msg = $this->load->view('partial/notification_email', $_POST, TRUE);

            $success = false;

            //$success = $this->mail_send('svjetlostmont@t-com.me',$msg);
            $success = $this->mail_send('ikukic@yahoo.com',$msg);

            if($success) {
                echo json_encode(array('action'=>true,'msg'=>'Succefull. Thank you for contacting us.'));
            }else{
                echo json_encode(array('action'=>false,'msg'=>'Something wrong. Please try again'));
            }

        }

        function mail_send($to,$message){

            $from = 'no-reply@globtourmontenegro.me';
            $subject = 'Notification Email - Globtour Montenegro';

            $headers  = "From: $from\r\n";
            $headers .= "Content-type: text/html\r\n";

            return mail($to, $subject, $message, $headers); //0 or 1

        }


}